const app = getApp();
const createBackIcon = () => {
  return {
    icon: 'https://ossbaojie.bojem.com/bj/strollHome/back.png',
    size: 9,
    left: 18,
    opacity: 0
  };
};
const createTitleIcon = () => {
  return {
    isEnable: true,
    icon: '',
    size: 40,
    text: '宝姐家',
    color: '#333333'
  };
};
Component({
  options:{
    addGlobalClass: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    isEnablePlace: {
      type: Boolean,
      value: true
    },
    backIcon: {
      type: Object,
      value: createBackIcon()
    },
    titleIcon: {
      type: Object,
      value: createTitleIcon()
    }
  },

  observers:{
    backIcon(val){
      const newBackIcon = Object.assign(this.data.newBackIcon, createBackIcon(), val);
      this.setData({ 
        newBackIcon
      })
    },

    titleIcon(val){
      const newTitleIcon = Object.assign(this.data.newTitleIcon, createTitleIcon(), val);
      this.setData({
        newTitleIcon
      })
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    navbarHeight: 44,
    topSafeHeight: 20,
    newTitleIcon: createTitleIcon(),
    newBackIcon: createBackIcon()
  },

  lifetimes: {
    ready() {
      const navbar = this.getNavbar();
      const {navbarHeight, topSafeHeight} = navbar;
      this.setData({ navbarHeight, topSafeHeight })
      this.triggerEvent('navbarNotify', navbar)
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getNavbar() {
      const { statusBarHeight, safeArea, screenHeight } = wx.getSystemInfoSync();
      //胶囊按钮
      const capsule = wx.getMenuButtonBoundingClientRect();
      const navbarHeight = capsule && capsule.bottom + capsule.top - statusBarHeight + 4 || statusBarHeight + 46;
      const bottomSafeHeight = screenHeight - safeArea.bottom;
      return {
        screenHeight, 
        navbarHeight,
        clientHeight: screenHeight - bottomSafeHeight - navbarHeight,
        topSafeHeight: statusBarHeight,
        bottomSafeHeight
      };
    },
    goBack() {
      wx.navigateBack({
        delta: 1,
        fail(){
          wx.reLaunch({
            url: app.globalData.home_path,
          })
        }
      })
    },
    //对象扁平化
    flatObj(data) {
      const target = {};
      const setItem = (data,name) => {
        Object.keys(data).forEach(key => {
            //将key值存储到target对象中
            let value = data[key];
            if(typeof value == 'object' && value != null){
              key = !name && key || name+'['+key+']';
              setItem(value,key);
            }else{
              key = !name && key || name+'['+key+']';
              target[key] = value
            }
        })
      };
      setItem(data);
      return target;
    }
  }
})
